<template>
	<view class="container" :style="{'height':containerHeight}">
		<view class="message">
			<view class="row1">
				<text v-if="userInfo.user.age">已实名认证</text>
				<text v-else>未实名认证</text>
			</view>
			<view class="row row2">
				<image :src="pic" mode="aspectFill" class="head"></image>
				<view @click="handleChangeAvatar()">
					<text> 修改头像 </text>
					<image src="https://images.linglinggong.net/static/newMy/staff-my4.png" mode="" id="img"></image>
				</view>
			</view>
			<view class="row row3" @click="goReal()">
				<text>姓名</text>
				<text v-if="userInfo.user.age">{{userInfo.realName}}</text>
				<text v-else>请实名认证</text>
			</view>
			<view class="row row4" @click="goReal()">
				<text>性别</text>
				<text v-if="userInfo.user.age">{{userInfo.user.sex}}</text>
				<text v-else>请实名认证</text>
			</view>
			<view class="row row5" @click="goReal()">
				<text>年龄</text>
				<text v-if="userInfo.user.age">{{userInfo.user.age}}</text>
				<text v-else>请实名认证</text>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				containerHeight: '',
				userInfo: {},
				pic: '',
				uploadToken: '',
				avatar: ''
			}
		},
		methods: {
			goReal() {
				uni.navigateTo({
					url: '/pages/real-name/real-name'
				})
			},
			async getUserInfo() {
				let res = await this.$fetch(this.$api.get_current_user, {}, 'GET')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				this.userInfo = res.data
				this.pic = uni.getStorageSync('imgPath') + (this.userInfo.user.avatarto || this.userInfo.user.avatar)
				if (this.userInfo.user.sex === '0') {
					this.userInfo.user.sex = '男'
				} else if (this.userInfo.user.sex === '1') {
					this.userInfo.user.sex = '女'
				}
			},
			// 修改头像，上传图片至七牛
			handleChangeAvatar() {
				uni.navigateTo({
					url: "/pages/camera/idphoto/idphoto"
				})
			},
			updateUserImage(e) {
				// console.log(e, "eeeeeeeeeeeeeeeeeeeeeeeee");
				this.pic = e
				uni.setStorageSync("pics", e)
				// this.headTesx = "审核中"
				// this.headStatus = 3
				this.uploadImgs(e)
			},
			// 上传图片至七牛
			uploadImgs(urls) {
				let _this = this
				let ext = urls.split(".").pop()
				// console.log(urls.split(".").pop())
				// let ext = urls.substring((urls.length-26),urls.length)
				uni.uploadFile({
					url: this.$api.uploadPic,
					name: 'file',
					filePath: urls,
					formData: {
						token: this.uploadToken,
						key: this.$u.guid() + "." + ext
					},
					success: ({
						data
					}) => {
						data = JSON.parse(data)
						// _this.userObj.avatar = data.hash
						// _this.userInfo.user.avatarto = data.hash
						_this.avatar = data.key
						this.handleSetUserInfo(1)
					}
				})
			},
			// 获取七牛上传TOKEN
			async get_qiniu_token() {
				let res = await this.$fetch(this.$api.get_qiniu_token, {}, 'POST')
				this.uploadToken = res.msg
			}, //确认修改用户信息
			async handleSetUserInfo(e) {
				let res = await this.$fetch(this.$api.upd_user, {
					avatar: this.avatar,
					adCode: uni.getStorageSync('region').slice(0, 6)
				}, 'POST')
				console.log("修改成功返回的", JSON.stringify(res))
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				if (this.userType == 1) {
					this.userTypeheadTesx = "修改完成"
				}
				uni.showToast({
					title: '修改成功~',
					icon: 'none'
				})
				if (e == 1 && this.userType != 1) {
					uni.showToast({
						title: '上传成功，等待审核',
						icon: 'none'
					})
					this.getUserInfo()
				}
				this.getUserInfo()
			}
		},
		onLoad() {
			this.containerHeight = uni.getWindowInfo().windowHeight + "px"
			this.getUserInfo()
		}
	}
</script>
<style lang="less" scoped>
	.container {
		background-color: #fff;
		padding: 30rpx;
		box-sizing: border-box;

		.message {
			width: 100%;
			height: 588rpx;
			border-radius: 24rpx;
			background-color: #F1F1F5;
			padding: 30rpx 20rpx;
			box-sizing: border-box;

			.row {
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 32rpx;
				font-weight: bold;

				text:first-child {
					color: #515151;
				}

				&>view {
					display: flex;
					justify-content: center;
					align-items: center;
				}

				#img {
					width: 32rpx;
					height: 32rpx;
				}

				text:last-child {
					color: #7F7F7F;
				}
			}

			.row1 {
				margin-bottom: 36rpx;

				text:first-child {
					font-size: 32rpx;
					color: #262626;
					font-weight: bold;
				}
			}

			.row2 {
				height: 96rpx;
				margin-bottom: 60rpx;

				image {
					width: 96rpx;
					height: 96rpx;
					border-radius: 50%;
				}
			}

			.row3 {
				margin-bottom: 76rpx;
			}

			.row4 {
				margin-bottom: 86rpx;
			}
		}
	}
</style>